package com.ruoyi.user.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.user.service.impl.CarUserServiceImpl;
import com.ruoyi.user.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.user.domain.CarUser;
import com.ruoyi.user.service.ICarUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户列表Controller
 * 
 * @author ruoyi
 * @date 2025-05-10
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class CarUserController extends BaseController
{
    @Autowired
    private ICarUserService carUserService;

    @Autowired
    private CarUserServiceImpl carUserServiceImpl;

    @Autowired
    private RemoteUserService remoteUserService;
    /**
     * 查询用户列表列表
     */
    @RequiresPermissions("user:user:list")
    @GetMapping("/list")
    public TableDataInfo list(CarUser carUser)
    {
        startPage();
        List<CarUser> list = carUserService.selectCarUserList(carUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表列表
     */
    @RequiresPermissions("user:user:export")
    @Log(title = "用户列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarUser carUser)
    {
        List<CarUser> list = carUserService.selectCarUserList(carUser);
        ExcelUtil<CarUser> util = new ExcelUtil<CarUser>(CarUser.class);
        util.exportExcel(response, list, "用户列表数据");
    }

    /**
     * 获取用户列表详细信息
     */
    @RequiresPermissions("user:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carUserService.selectCarUserById(id));
    }

    /**
     * 新增用户列表
     */
    @RequiresPermissions("user:user:add")
    @Log(title = "用户列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarUser carUser)
    {
        return toAjax(carUserService.insertCarUser(carUser));
    }


    /**
     * 新增用户列表
     */
//    @RequiresPermissions("user:user:add")
    @Log(title = "用户列表", businessType = BusinessType.INSERT)
    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody CarUser carUser)
    {
        carUser.setRegTime(new Date());
        System.out.println(carUser.toString());
        SysUser sysUser = new SysUser();
        sysUser.setUserName(carUser.getUserName());
        sysUser.setNickName(carUser.getNickName());
        sysUser.setPassword(carUser.getPassword());
        sysUser.setPhonenumber(carUser.getPhone());
        sysUser.setEmail(carUser.getEmail());
        sysUser.setLoginIp("127.0.0.1");
        Long[] roleIds = {101L};
        sysUser.setRoleIds(roleIds);
        System.out.println(sysUser.toString());
//        SysUser sysUser1 = remoteUserService.addSysUser(sysUser, SecurityConstants.INNER);
//        carUser.setSysId(sysUser1.getUserId());
//        return toAjax(carUserService.insertCarUser(carUser));
        return toAjax(1);
    }

    /**
     * 修改用户列表
     */
    @RequiresPermissions("user:user:edit")
    @Log(title = "用户列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarUser carUser)
    {
        return toAjax(carUserService.updateCarUser(carUser));
    }

    /**
     * 删除用户列表
     */
    @RequiresPermissions("user:user:remove")
    @Log(title = "用户列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carUserService.deleteCarUserByIds(ids));
    }

    //通过用户名查询用户信息
    //内部鉴权
    @InnerAuth
    @GetMapping("/queryUserByUserName/{username}")
    public CarUser getUserInfoByUserName(@PathVariable("username") String username){
        System.out.println("username = " + username);
        return carUserServiceImpl.queryCarUserByUserName(username);
    }

    //通过id查询用户信息
    @InnerAuth
    @GetMapping("/queryUserById/{id}")
    public CarUser getUserInfoById(@PathVariable("id") Long id){
        return carUserServiceImpl.selectCarUserById(id);
    }
}
